package com.riddle.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.riddle.model.Permission;
import com.riddle.model.Role;
import com.riddle.util.StringUtil;
import com.riddle.vmodel.ValuePair;

@Repository
@Scope("prototype")  
public class RoleDao extends BaseDao<Role>{

	
	public List<Role> queryParent(){
		return super.query("selectParentRole");
	}
	
	public Role query(Long id){
		return super.query(id, "selectRoleById");
	}
	
	public List<Role> queryModelByPage(Integer page, Integer rows, String query){
		String statement = null;
		if(StringUtil.isNullOrEmpty(query)) {
			statement = "selectRoleTree";
		} else {
			statement = "selectRoleForQuery";
		}
		return super.queryModelByPage(page, rows, query, statement);
	}
	
	public void insert(Role model) {
		super.insert(model, "insertRole");
	}
	
	public void update(Role model) {
		super.update(model, "updatetRole");
	}
	
	public void delete(Long id) {
		super.delete(id, "deleteRole");
	}
	
	public List<Role> getRoleTree(){
		return super.query("selectRoleTree");
	}
	
	public Role getRoleWithPermission(Long id){
		return super.query(id, "selectRoleWithPermission");
	}
	
	public void deleteRolePermission(Long id) {
		super.delete(id, "deleteRolePermission");
	}
	
	public void saveRolePermission(List<ValuePair<Long, String>> list) {
		try(SqlSession session = openSession()){
			session.insert("insertRolePermission", list);
		}
	}
}
